﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WpfPractice.Model;
using WpfPractice.ViewModel;

namespace WpfPractice.View
{
    // View的后台代码
    // 增删操作代码
    public partial class TestWindow : Window
    {
        private TestWindowViewModel vm;
        public TestWindow()
        {
            InitializeComponent();
            this.DataContext = vm = new TestWindowViewModel();
        }
        private void ButtonAdd_OnClick(object sender, RoutedEventArgs e)
        {
            vm.NamesCollection.Add(new Student()
            {
                Name = $"Test-{new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds()}",
                Age = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds() / 1000,
                Grades = $"{new Random().Next(5, 10)}年级·{new Random().Next(1, 10)}班",
            });
        }
        private void ButtonRemove_OnClick(object sender, RoutedEventArgs e)
        {
            vm.NamesCollection.RemoveAt(0);
        }
        private void ButtonMove_OnClick(object sender, RoutedEventArgs e)
        {
            vm.NamesCollection.Move(vm.NamesCollection.Count - 1, 0);
        }
        private void ButtonClear_OnClick(object sender, RoutedEventArgs e)
        {
            vm.NamesCollection.Clear();
        }
        private void ButtonSearch_OnClick(object sender, RoutedEventArgs e)
        {
            SearchCommand();
        }
        private void TextBoxBase_OnTextChanged(object sender, TextChangedEventArgs e)
        {
            SearchCommand();
        }
        private void SearchCommand()
        {
            vm.DisplayName = new ObservableCollection<Student>(vm.NamesCollection.Where(student =>
                (student.Name.Contains(vm.SearchContent) || student.Age.ToString().Contains(vm.SearchContent) ||
                 student.Grades.Contains(vm.SearchContent))).ToList());
        }
    }

}
